﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.IO;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Commands.Behaviors.CodeAnalysis.Metrics
{
    public class ValidateMetricsAvailabilityBehavior : ICommandBehavior<CodeAnalysisCommandExecutionContext>
    {
        private IUnityContainer container;


        public ValidateMetricsAvailabilityBehavior(IUnityContainer container)
        {
            this.container = container;
        }


        public void Behave(CodeAnalysisCommandExecutionContext context)
        {
            var package = container.Resolve<IFxCopIntegratorPackage>();
            var option = package.GeneralOption;

            if (!File.Exists(option.MetricsPath))
            {
                context.Cancel("FxCop Integrator didn't perform code analysis because it couldn't find Metrics.exe.", () => container.Resolve<IFxCopIntegratorPackage>().ShowGeneralOptionDialog());
                return;
            }

            context.Progress.MetricsVersion = option.MetricsVersion;
        }
    }
}
